package com.y2.authprj.mvc.controller.auth;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gdglc.spring.biz.auth.StaffBiz;
import com.gdglc.spring.biz.impl.auth.StaffBizImpl;
import com.github.pagehelper.PageInfo;
import com.y2.authprj.bean.auth.model.StaffInfo;
import com.y2.authprj.bean.auth.vo.ResponeVo;
import com.y2.authprj.bean.auth.vo.StaffVo;
import com.y2.authprj.exception.BizException;
import com.y2.authprj.exception.UpdateException;
import com.y2.authprj.utils.DES3LEncrypt;

@Controller
@RequestMapping("/auth/staff")
public class StaffController {
	@Resource(name="tstaffBiz")
	private StaffBiz staffBiz;
 
	public StaffBiz getStaffBiz() {
		return staffBiz;
	}

	public void setStaffBiz(StaffBiz staffBiz) {
		this.staffBiz = staffBiz;
	}
	public StaffController(){
		System.out.println("初始化StaffController");
	}
	
	/*
	//多条件查询 by 辉 控制层例子 别直接用 参数要改的 这只是例子
	@RequestMapping("/page")
	public String page(Model model,@RequestParam(
			required=false,defaultValue = "1")int num,
			StaffInfo staffInfo){
		PageInfo<StaffInfo> queryByPage = staffBiz.queryByPage(staffInfo, num, 15);
		List<StaffInfo> staffInfos = queryByPage.getList();
		return "index";
	}
	*/
	@RequestMapping("/outLogin")
	public String outLogin(Model model,HttpSession session){
		
		session.removeAttribute("findByStaffName");
		return "/auth/staff/login";
	}
	
	
	
	@RequestMapping("/login")
	public String login(String userName,String pwd,Model model,HttpSession session){
		if(userName==null && pwd==null){
			model.addAttribute("msg", "");
		}else{
			try {
				int verifyLoginByStaffNumAndPwd = staffBiz.verifyLoginByStaffNumAndPwd(userName, pwd);
				if(verifyLoginByStaffNumAndPwd>0){
					StaffInfo findByStaffName = staffBiz.findByStaffName(userName);
					StaffInfo staffInfo=new StaffInfo();
					staffInfo.setStaffNum(userName);
					staffInfo.setLoginTime(new Date());
					try {
						staffInfo.setLoginIp(InetAddress.getLocalHost().toString());
					} catch (UnknownHostException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					int i=staffBiz.updateIPandTime(staffInfo);
					System.out.println(i);
					session.setAttribute("findByStaffName", findByStaffName);
					model.addAttribute("tstaffInfos",staffInfo);
					return "redirect:/auth/functionauthority/list";
				}else{
					model.addAttribute("msg", "用户名或密码错误");
				}
			} catch (BizException e) {
				model.addAttribute("msg", e.getMessage());
			}
			model.addAttribute("userName",userName);
			model.addAttribute("pwd",pwd);
		}
		return "/auth/staff/login";
		
	}
	@RequestMapping("/page")
	public String page(Model model,@RequestParam(
			required=false,defaultValue = "1")int pageNum,
			StaffInfo staffInfo){
		PageInfo<StaffInfo> queryByPage = staffBiz.queryByPage(staffInfo, pageNum, 5);
		
		List<StaffInfo> staffInfos = queryByPage.getList();
		
		System.out.println(queryByPage);
		System.out.println(staffInfos);
		model.addAttribute("queryByPage",queryByPage);
		model.addAttribute("tstaffInfos",staffInfos);
		return "/auth/staff/tstaffList";
	}
	
	@RequestMapping("/findAll")
	public String findAll(Model model){
		List<StaffInfo> tstaffInfos;
		StaffInfo staffInfo=new StaffInfo();
		try {
			tstaffInfos = staffBiz.findAll();
			model.addAttribute("tstaffInfos",tstaffInfos);
			model.addAttribute("id",staffInfo.getStaffId());
		} catch (BizException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return "/auth/staff/tstaffList";
	}
	
	public void initInfo(Model model){
		try {
			List<StaffInfo> tstaffInfos = staffBiz.findAll();
			model.addAttribute("tstaffInfos",tstaffInfos);
		} catch (BizException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	@RequestMapping("/addStaff")
	public String addStaff(StaffInfo staffInfo,Model model){
		java.sql.Date currentDate = new java.sql.Date(System.currentTimeMillis());
		staffInfo.setChangePwd(1);
		staffInfo.setDeleteFlag(1);
		staffInfo.setCreateTime(new Date());
		try {
			staffInfo.setLoginIp(InetAddress.getLocalHost().toString());
		} catch (UnknownHostException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		staffInfo.setLoginTime(new Date());
		staffInfo.setLogStatus(1);
		
		
		try {
			staffBiz.addStaff(staffInfo);
		} catch (BizException e) {
			// TODO Auto-generated catch block
			model.addAttribute("msg",e.getMessage());
		}
		/*initInfo(model);*/
		return "redirect:/auth/staff/page";
	}
	
	@RequestMapping("/list")
	public String findByStaff(StaffVo staffVo,Model model){		
		List<StaffInfo> tstaffInfos = staffBiz.findByStaff(staffVo);
		model.addAttribute("tstaffInfos",tstaffInfos);
	
		/*initInfo(model);*/
		return "/auth/staff/tstaffList";
	}
	
	@RequestMapping("/del")
	@ResponseBody
	public String del(String[] delitems,Model model){
		for (int i = 0; i < delitems.length; i++) {
			try {
				staffBiz.deleteStaff(Integer.parseInt(delitems[i]));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return "";
	}
	
	@RequestMapping("/isExitsNum")
	@ResponseBody
	public String isExitsNum(String staffNum,Model model){
		int i=0;
		try {
			i=staffBiz.findByStaffNum(staffNum);
			if(i>0){
				return "false";
			}
				
			
		} catch (BizException e) {
			//
			e.printStackTrace();
		}
		return "true";
		
	}
	
	@RequestMapping("/isExitsPwd")
	@ResponseBody
	public String verifyLoginByStaffNumAndPwd(String staffNum,String pwd,Model model){
		int i=0;
		try {
			i=staffBiz.verifyLoginByStaffNumAndPwd(staffNum, pwd);
			if(i>0){
				return "true";
			}
				
			
		} catch (BizException e) {
			//
			e.printStackTrace();
		}
		return "false";
		
	}
	
	@RequestMapping("/isExitsChname")
	@ResponseBody
	public String isExitsChname(String chnName,String surName,Model model){
		int i=0;
		try {
			i=staffBiz.findByStaffChname(chnName,surName);
			if(i>0){
				return "true";
			}
				
			
		} catch (BizException e) {
			//
			e.printStackTrace();
		}
		return "false";
		
	}
	
	//详细
	@RequestMapping("/detail")
	public String detail(Integer staffId,Model model){
		try {
			StaffInfo info = staffBiz.show(staffId);
			model.addAttribute("info", info);
/*			String szSrc = "123456789:"+info.getPwd();
			DES3LEncrypt.getInstance();
			
			byte[] encrypt2 = DES3LEncrypt.encrypt(szSrc);
			String encodedString =new String(encrypt2,"utf-8");
			System.out.println(encodedString);
			
	        String value = DES3LEncrypt.decrypt(szSrc);//调用解密方法  
	        value =  value.substring(value.indexOf(':')+1);
	        System.out.println(value);
	        
	        model.addAttribute("infos",value);*/
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/auth/staff/detail";
	}
	
	@RequestMapping("/updatePwd")
	public String updatePwd(StaffInfo tstaff,Model model){
		try {
			staffBiz.updateStaff(tstaff);
		} catch (UpdateException e) {
			model.addAttribute("errorMessage", e.getMessage());
		}catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/auth/staff/page";
	}
	
	@RequestMapping("/ajax/updatePwd")
	@ResponseBody
	public ResponeVo ajaxUpdate(StaffInfo tstaffInfo,String test){
		ResponeVo vo=new ResponeVo();
		try {
			staffBiz.updateStaff(tstaffInfo);
		} catch (UpdateException e) {
			vo.setCode(ResponeVo.ERROR);
			vo.setMessage(e.getMessage());
		}catch (Exception e) {
			e.printStackTrace();
			vo.setCode(ResponeVo.ERROR);
			vo.setMessage("执行失败!");
		}
		return vo;
	}
}
